limsReport.auditPage = {

    getGridParam: function () {
        var _this = this;
        return {
            type: 'grid',
            id: 'lims-report-audit-list-grid',
            url: limsReport.baseUrl + '/queries/tasks',
            columns: limsReport.getAuditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.getAsyncComp('lims-report-uploader-audit').done(function (grid) {
                        grid.setOptions({
                            bizId: -1
                        })
                    });
                }
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp('lims-report-uploader-audit').done(function (grid) {
                    grid.setOptions({
                        bizId: row.id
                    })
                });
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.PASS',
                icon: 'pass',
                onClick: function () {
                    _this.pass();
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                icon: 'reject',
                onClick: function () {
                    _this.reject();
                }
            }, {
                type: 'button',
                text: 'GIKAM.REPORT.ONLINE.VIEW',
                icon: 'preview',
                onClick: function () {
                    var grid = Gikam.getComp('lims-report-audit-list-grid');
                    var selections = grid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    window.open(limsReport.baseUrl + '/preview-online/reports2' + Gikam.param({
                        ids: selections.map(function (item) {
                            return item.id;
                        }).join(',')
                    }));
                }
            }]
        }
    },

    pass: function () {
        var grid = Gikam.getComp('lims-report-audit-list-grid');
        Gikam.create('workflow').pass({
            data: grid.getSelections(),
            pageObject: limsReport,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            grid.refresh();
        });
    },

    reject: function () {
        var grid = Gikam.getComp('lims-report-audit-list-grid');
        Gikam.create('workflow').reject({
            data: grid.getSelections(),
            pageObject: limsReport,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            grid.refresh();
        });
    },

    getBaseInfoTab: function () {
        var _this = this;
        return {
            type: 'tab',
            panels: [{
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'lims-report-uploader-audit',
                    dbTable: 'T_LIMS_REPORT',
                    readonly: true
                }]
            }]
        }
    },

    create: function () {
        var _this = this;
        Gikam.create('layout', {
            id: 'lims-report-audit-list-layout',
            renderTo: workspace.window.$dom,
            north: {
                height: '60%',
                items: [this.getGridParam()]
            },
            center: {
                items: [this.getBaseInfoTab()]
            }
        });
    },

    init: function (param) {
        this.param = param;
        this.create();
    }
};
